package llTest;
//v_2.0.0_build_20161024
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import de.bwaldvogel.liblinear.*;

public class Data {
	int pointNumber=0;
	List<Feature[]> featureArrayList=new ArrayList<>();
	List<Integer> targetValue=new ArrayList<>();
	int featureDimension=0;
	public void loadData(String filePath) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
        for (String line = br.readLine(); line != null; line = br.readLine()) {
        	if(line.length()>0){
            	String[] dataString=line.split(" ");
            	targetValue.add(Integer.parseInt(dataString[0]));
            	int j=1;
            	List<FeatureNode> featureNodes=new ArrayList<>();
            	while(j<dataString.length){
            		String[] nodeData=dataString[j].split(":");
            		int k=Integer.parseInt(nodeData[0]);
            		featureNodes.add(new FeatureNode(k+1, Double.parseDouble(nodeData[1])));
            		if(featureDimension<k+1){
            			featureDimension=k+1;
            		}
            		j++;
            	}
            	
            	Feature[] f=new Feature[featureNodes.size()];
            	for(int k=0;k<featureNodes.size();k++){
            		f[k]=(Feature)featureNodes.get(k);
            	}
            	featureArrayList.add(f);
        	}
        }
        pointNumber=featureArrayList.size();
        br.close();
	}
	
	public int getFeatureDimension() {
		return featureDimension;
	}
	public Feature[][] getFeatureMatrix() {
		Feature[][] featureMatrix=new Feature[pointNumber][];
		for(int i=0;i<pointNumber;i++){
			featureMatrix[i]=featureArrayList.get(i);
		}
		return featureMatrix;
	}
	
	public Feature[] getSample(int index){
	Feature[] sample=featureArrayList.get(index);
	return sample;
}

	public int getPointNumber(){
		return pointNumber;
	}

	public double[] getTargetValue() {
		double[] tV=new double[pointNumber];
		for(int m=0;m<pointNumber;m++){
			tV[m]=targetValue.get(m);
		}
		return tV;
	}
}
